<template>
  <div class="app-container">
    <!-- 高级查询 -->
    <div class="filter_container">

     </div>
    <!-- <el-form v-show="searchVisible" ref="queryForm" :model="queryParams" :inline="true">
     <el-form-item label="订单号:">
       <el-input v-model="queryParams.salesOrderNo" size="small" placeholder="请输入订单号" clearable @keyup.enter.native="handleQuery" />
     </el-form-item>
     <el-form-item label="客户订单号:">
       <el-input v-model="queryParams.custSalesOrderNo" size="small" placeholder="请输入订单号" clearable @keyup.enter.native="handleQuery" />
     </el-form-item>
     <el-form-item label="客户子订单号:">
       <el-input v-model="queryParams.custSalesOrderItemNo" size="small" placeholder="请输入订单号" clearable @keyup.enter.native="handleQuery" />
     </el-form-item>
     <el-form-item label="商城:">
       <el-input v-model="queryParams.customerName" size="small" placeholder="请输入收货人" clearable @keyup.enter.native="handleQuery" />
     </el-form-item>
     <el-form-item label="店铺:">
       <el-input v-model="queryParams.storeName" size="small" placeholder="请输入收货人手机" clearable @keyup.enter.native="handleQuery" />
     </el-form-item>

      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form> -->

    <!-- 工具栏 -->
    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button v-hasPermission="['order:salesOrder:insert']" type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button v-hasPermission="['order:salesOrder:insert']" type="info" icon="el-icon-upload2" size="mini" @click="openUploadDialog">批量拉动单导入</el-button>
      </el-col>
     <el-col :span="1.5">
        <el-button v-hasPermission="['order:salesOrder:insert']" type="info" icon="el-icon-upload2" size="mini" @click="retrieveOrders">获得新订单</el-button>
      </el-col>
      <pagination v-show="page.total>0" :total="page.total" :page.sync="page.current" :limit.sync="page.pageSize" @pagination="handleQuery" />

  <!--    <right-toolbar :show-search.sync="searchVisible" @queryTable="handleQuery" />
  -->  </el-row>

    <!-- 数据列表 -->

   <el-table v-loading="loading" :data="page.records" highlight-current-row @selection-change="handleSelectionChange">
     <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="单号" width="467" align="left" prop="location" />
      <el-table-column label="出错原因" width="355" align="left" prop="errorMsg" />
      <el-table-column label="出错代码" align="left" prop="method" />
      <el-table-column label="操作类型" align="left" prop="operationType" />

      <el-table-column label="创建时间" align="left" prop="createTime" width="180">
       <template slot-scope="scope">
         <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
       </template>
     </el-table-column>
     <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
       <template slot-scope="scope">
         <el-button size="mini" type="text" icon="el-icon-delete" @click="handleRemove(scope.row)">删除</el-button>
       </template>
     </el-table-column>
   </el-table>
    <!-- 分页 -->
  <!-- 导入对话框 -->
    <el-dialog :title="uploadDialog.title" :visible.sync="uploadDialog.visible" width="400px" append-to-body>
      <div v-loading="uploadLoading">
        <el-upload
          ref="upload"
          :limit="1"
          :headers="uploadData.headers"
          :action="uploadData.url + '?updateSupport=' + uploadData.updateSupport"
          :data="uploadData.params"
          :disabled="uploadData.isUploading"
          :on-progress="handleFileUploadProgress"
          :on-success="handleFileSuccess"
          :on-error="handleFileError"
          :auto-upload="false"
          accept=".xlsx, .xls"
          drag
        >
          <i class="el-icon-upload" />
          <div class="el-upload__text">
            将文件拖到此处，或
            <em>点击上传</em>
          </div>
          <!-- <div slot="tip" class="el-upload__tip">
            <el-select v-model="uploadData.params.customerNo" style="width: 100%" filterable clearable placeholder="请选择下单客户">
              <el-option v-for="option in options.customers" :key="option.id" :label="option.customerName" :value="option.id" />
            </el-select>
          </div> -->
         <div slot="tip" class="el-upload__tip">
            <el-link type="info" style="font-size:16px" @click="handleDownloadTemplate">下载模板</el-link>
          </div>

          <div slot="tip" class="el-upload__tip" style="color:red">提示：仅允许导入“xls”或“xlsx”格式文件！</div>
        </el-upload>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="confirmUploadDialog">确 定</el-button>
          <el-button @click="resetUploadDialog">取 消</el-button>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
 import OperationLogApi from '@/api/system/OperationLogApi'
import SalesOrderApi from '@/api/order/SalesOrderApi'
import AccountingApApi from '@/api/accounting/AccountingApApi'
import SalesOrderItemApi from '@/api/order/SalesOrderItemApi'
import CustomerApi from '@/api/basic/CustomerApi'
import DictValueApi from '@/api/system/DictValueApi'
import appConstant from '@/constant/AppConstant'
import AuthUtils from '@/utils/AuthUtils'
export default {
  name: 'OmsSalesOrderList',
  filters: {
    settleStatusFilter(item) {
      const values = appConstant.settleStatusList.filter(value => value.key === item)
      if (values && values.length > 0) {
        return values[0].label
      } else {
        return ''
      }
    }
   },
  data() {
    return {
      options: {
        sysYnTypeOptions: [],
        orderTypeOptions: [],
        sourceTypeOptions: [],
        settleStatusOptions: appConstant.settleStatusList,
        salesOrderStatusOptions: [],
        customers: [],
        payTypeOptions: []
      },
      queryParams: {
        operationType: null,
        isValid: this.$constant.Y
      },
      page: {
        current: 1,
        pageSize: 10,
        total: 0,
        records: []
      },
      uploadDialog: {
        title: '客户拉动单导入',
        visible: false
      },
      uploadData: {
        headers: { Authorization: 'Bearer ' + AuthUtils.getToken() }, // 上传请求头
        url: process.env.VUE_APP_BASE_API + '/web/tbWmsMaterialDeliveryLine/importData', // 上传的地址
        params: {
        //  customerNo: '1353968310689075200' // 会员商城
        },
        isUploading: false, // 是否禁用上传
        updateSupport: 0 // 是否更新已经存在的数据
      },
      loading: true,
      uploadLoading: false,
      searchVisible: true,
      multipleSelection: [],
      isSingle: true,
      isMultiple: true
    }
  },
  created() {
    this.handleQuery()
    this.loadOptions()
  },
  methods: {
    loadOptions() {
      CustomerApi.list().then(response => {
        this.options.customers = response.data
      })
      DictValueApi.list({ dictCode: 'oms_sales_order_type' }).then(response => {
        this.options.orderTypeOptions = response.data
      })
      DictValueApi.list({ dictCode: 'oms_sales_order_source' }).then(response => {
        this.options.sourceTypeOptions = response.data
      })
      DictValueApi.list({ dictCode: 'oms_sales_order_status' }).then(response => {
        this.options.salesOrderStatusOptions = response.data
      })
      DictValueApi.list({ dictCode: 'sys_payment_type' }).then(response => {
        this.options.payTypeOptions = response.data
      })

    },
    handleAdd() {
      this.$router.push('/salesOrder/form/')
    },
    handleApAmtBtnShow: function(status) {
      const validStatusList = ['depart', 'delivery', 'poded', 'submitted']
      const index = validStatusList.indexOf(status)
      if (index === -1) {
        return false
      } else {
        return true
      }
    },
    handleImport(row) {
      this.$confirm('是否修复导入问题?', '警告', {
        confirmButtonText: '再次导入',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        SalesOrderItemApi.importAgain(row.id)
      }).then(() => {
        this.handleQuery()
        this.$message.success('修复及导入完成')
      })
    },
    handleRemove(row) {
      this.$confirm('是否确认删除数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return SalesOrderApi.remove(row.id)
      }).then(() => {
        this.handleQuery()
        this.$message.success('删除成功')
      })
    },
    generateSettlement() {
       AccountingApApi.generateArAp().then(() => {
         this.$message.success('生成结算成功!')
         this.handleQuery()
       })
    },
    retrieveOrders() {
       SalesOrderItemApi.retrieveOrders().then(() => {
         this.$message.success('获取订单成功!')
         this.handleQuery()
       })
    },
    handleEdit(row) {
      this.$router.push({ path: '/salesOrder/form/' + row.id })
    },
   handleQuery() {
     this.loading = true
     this.queryParams.operationType = "订单导入";
     OperationLogApi.page(this.queryParams, this.page).then(response => {
       this.page.records = response.data.records
       this.page.total = response.data.total
       this.loading = false
     }, () => {
       this.loading = false
     })
   },
    handleExport() {
      this.$confirm('是否确认导出所有销售订单数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return SalesOrderItemApi.exportData(this.queryParams)
      }).then(response => {
        this.$common.download(response.data)
      })
    },
    handleSelectionChange(selection) {
      this.multipleSelection = selection
      this.isSingle = selection.length !== 1
      this.isMultiple = !selection.length
    },
    openUploadDialog() {
      this.resetUploadDialog()
      this.uploadDialog.visible = true
    },
    confirmUploadDialog() {
      // this.uploadLoading = true
      // this.$refs.upload.submit()
    //  if (this.uploadData.params.customerNo) {
        this.uploadLoading = true
        this.$refs.upload.submit()
    //  } else {
   //     this.$message.warning('请先选择客户')
   //   }
    },
    resetUploadDialog() {
      this.uploadDialog.visible = false
    //  this.uploadData.params.customerNo = '1353968310689075200' // 会员商城
    },
    handleDownloadTemplate() {
      SalesOrderApi.importTemplate().then(response => {
        this.$common.download(response.data)
      })
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.uploadData.isUploading = true
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.uploadLoading = false
      this.uploadDialog.visible = false
      this.uploadData.isUploading = false
      this.$refs.upload.clearFiles()
      this.$alert(response.data ? response.data : response.msg, '导入结果', { dangerouslyUseHTMLString: true })
      this.queryParams.status = null
      this.handleQuery()
    },
    // 文件上传失败处理
    handleFileError(response, file, fileList) {
      this.uploadLoading = false
    },
    resetQuery() {
      this.page.current = 1
      this.queryParams = {
        custSalesOrderNo: null,
        custSalesOrderItemNo: null,
        customerName: null,
        storeName: null,
        isValid: this.$constant.Y
      }
      this.handleQuery()
    },
    orderTypeFormat(type) {
      return this.$common.getDictValueName(this.options.orderTypeOptions, type)
    },
    settleStatusFormat(type) {
      return this.$common.getDictValueName(this.options.settleStatusOptions, type)
    },
    orderSourceFormat(source) {
      return this.$common.getDictValueName(this.options.sourceTypeOptions, source)
    },
    orderStatusFormat(status) {
      return this.$common.getDictValueName(this.options.salesOrderStatusOptions, status)
    },
    payTypeFormat(type) {
      return this.$common.getDictValueName(this.options.payTypeOptions, type)
    }
  }
}
</script>
